package com.zzld.modules.maintenance.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.jsoup.helper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.zzld.core.utils.MessageUtils;
import com.zzld.core.utils.UuidUtil;
import com.zzld.modules.common.dto.CommonJSON;
import com.zzld.modules.common.dto.TreeGrid;
import com.zzld.modules.maintenance.entity.Brand;
import com.zzld.modules.maintenance.service.IBrandService;
import com.zzld.modules.sys.security.ShiroUser;

@Controller
@RequestMapping("/maintenance/brand")
public class BrandController {

	private String listurl = "/modules/maintenance/brand/brandList";
	private String licru = "/modules/maintenance/brand/brandCru";

	@Autowired
	private IBrandService brandService;

	@RequestMapping(method = RequestMethod.GET)
	public String list() {
		return listurl;
	}

	@RequestMapping("/Cru")
	public String cru(HttpServletRequest request) {
		return licru;
	}

	@RequestMapping(value="/update/pro",method=RequestMethod.POST)
	@ResponseBody
	public CommonJSON update(HttpServletRequest request, Brand brand) {
		CommonJSON json = new CommonJSON();
		ShiroUser userq = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		try {
			if (StringUtil.isBlank(brand.getBrandId())) {
				// 新增
				brand.setInnertime(new Date());
				brand.setCreateby(userq.getName());
				brand.setBrandId(UuidUtil.uuid().toString());
				if (brandService.save(brand) > 0) {
					json.setMessage(MessageUtils.ADD_SUCCESS);
					json.setSuccess(true);
				} else {
					json.setMessage(MessageUtils.ADD_FAIL);
					json.setSuccess(false);
				}
			} else {
				// 修改
				if (brandService.updateByPrimaryKeySelective(brand) > 0) {
					json.setMessage(MessageUtils.EDIT_SUCCESS);
					json.setSuccess(true);
				} else {
					json.setMessage(MessageUtils.EDIT_WARING);
					json.setSuccess(false);
				}
			}
		} catch (Exception e) {
			json.setMessage(MessageUtils.ADD_FAIL);
			json.setSuccess(false);
		}
		return json;
	}

	@RequestMapping(value="/del/{id}",method=RequestMethod.POST)
	@ResponseBody
	public CommonJSON delbyid(HttpServletRequest request,
			@RequestParam("id") String id) {
		CommonJSON json = new CommonJSON();
		try {
			if (id != null && !id.equals("")) {
				if (brandService.deleteByPrimaryKey(id) > 0) {
					json.setMessage(MessageUtils.DELETE_SUCCESS);
					json.setSuccess(true);
				}
			}
		} catch (Exception e) {
			json.setMessage(MessageUtils.DELETE_WARNING);
			json.setSuccess(false);
		}
		return json;
	}

	@RequestMapping("ser/{id}")
	@ResponseBody
	public JSONObject ser(HttpServletRequest request,
			@RequestParam("id") String id) {
		JSONObject json = new JSONObject();
		try {
			if (StringUtils.isNoneBlank(id)) {

				Brand entity = brandService.selectByPrimaryKey(id);
				json.put("brandId", entity.getBrandId());
				json.put("brandname", entity.getBrandname());
				json.put("pid", entity.getPid());
				json.put("letter", entity.getLetter());
				json.put("type", entity.getType());
				json.put("sort", entity.getSort());
				json.put("state", entity.getState());
				json.put("images", entity.getImages());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json;
	}

	@RequestMapping("/treegrid")
	@ResponseBody
	public List<TreeGrid> grid(HttpServletRequest request) {
		return brandService.treegrid();
	}
}
